﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using NS.Model.Systems;
using NS.BLL.Systems;
using NS.Utilities;

public partial class Systems_MenuRole : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataShow();
        }
    }
    private void DataShow()
    {
        List<Role> RoleList = RoleService.GetRoleByWeights(100);

        List<string> MenuRole = PowerService.GetRolesByMenuID(Request.QueryString["ID"]);
        foreach (Role Model in RoleList)
        {
            string rolename = Model.RoleName;
            ListItem li = new ListItem(rolename, rolename);
            if (MenuRole.Contains(rolename))
            {
                li.Selected = true;
            }
            ChBLRoles.Items.Add(li);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        NS.BLL.Systems.PowerService.DeletePowerByMenuID(Request.QueryString["ID"]);
        foreach (ListItem li in ChBLRoles.Items)
        {
            if (li.Selected)
            {
                NS.Model.Systems.Power Model = new NS.Model.Systems.Power();
                Model.MenuID = Int64.Parse(Request.QueryString["ID"]);
                Model.RolesName = li.Value;
                NS.BLL.Systems.PowerService.Insert(Model);
            }
        }
        MessageBox.AppendScript(this, "$(':input[id$=\"btnBindData\"]', window.parent.document.body).click();");
    }
}